package br.edu.senai.controller;

import br.edu.senai.model.Cliente;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Query;

public class ClienteDAO implements Serializable{

    private final Conexao conexao = new Conexao();

    public void salvar(Cliente c) {
        if (c.getId() == null) {
            conexao.persist(c);
        } else {
            conexao.alterar(c);
        }
    }

    public void remover(Cliente c) {
        conexao.excluir(c);
    }

    public List<Cliente> listar() {
        conexao.conectar();
        Query q = conexao.getEm().createQuery("select c from tb_cliente c");
        List<Cliente> lCliente = q.getResultList();
        conexao.desconectar();
        return lCliente;
    }

    public Cliente find(Long id) {
        conexao.conectar();
        Cliente c = conexao.getEm().find(Cliente.class, id);
        conexao.desconectar();
        return c;
    }
}
